package com.yiyang.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yiyang.domain.po.Bed;
import com.yiyang.domain.po.Room;
import com.yiyang.domain.vo.BedVo;
import com.yiyang.domain.vo.RoomVo;
import com.yiyang.mapper.BedMapper;
import com.yiyang.service.RoomService;
import com.yiyang.mapper.RoomMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author itcast
* @description 针对表【room】的数据库操作Service实现
* @createDate 2024-12-19 11:17:05
*/
@Service
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room>
    implements RoomService{

    @Autowired
    private RoomMapper roomMapper;

    @Autowired
    private BedMapper bedMapper;

    @Override
    public RoomVo findCwsyBedVo(String floor) {
        LambdaQueryWrapper<Room> lqw = new LambdaQueryWrapper<>();
        lqw.eq(!StringUtils.isEmpty(floor),Room::getRoomFloor,floor);
        //根据楼层获取房间信息
        List<Room> roomList = roomMapper.selectList(lqw);
        //最终返回的对象
        RoomVo roomVo = new RoomVo();

        List<BedVo> bedVoList = new ArrayList<>();

        for (Room room : roomList) {
            //新建bedVo封装房间和床位信息
            BedVo bedVo = new BedVo();
            BeanUtils.copyProperties(room,bedVo);

            LambdaQueryWrapper<Bed> bLqw = new LambdaQueryWrapper<>();
            bLqw.eq(Bed::getRoomNo,room.getRoomNo());
            //根据房间获取床位信息
            List<Bed> bedList = bedMapper.selectList(bLqw);
            //封装床位信息到bedVo对象中
            bedVo.setBedList(bedList);
            bedVoList.add(bedVo);
            // 总床位
            roomVo.setZcw(roomVo.getZcw() + bedList.size());

            for (Bed bed : bedList) {
                // 总空闲数
                if(bed.getStatus() == 1){
                    roomVo.setKx(roomVo.getKx() + 1);
                }
                // 总有人数
                if(bed.getStatus() == 2){
                    roomVo.setYr(roomVo.getYr() + 1);
                }
                // 总外出数
                if(bed.getStatus() == 3){
                    roomVo.setWc(roomVo.getWc() + 1);
                }
            }

        }

        roomVo.setRoomList(bedVoList);

        return roomVo;
    }
}




